<?php

require_once 'MessageCode.php';
require_once 'Card.php';

class Rule
{
    //牌的规则 1. 单 2.对子 3.3不带 4.3带1 5.炸弹 6.顺子 7.4带2 8.连队 9.飞机 10.对王 11.3带2
    public $myCards; //[cardId=>1]
    public $cardDetail;


    public function __construct($myCards)
    {
        $this->myCards = $myCards;
        $this->cardDetail = new Card();

    }

    /**
     * 单张牌
     * @param $myCards
     * @return int
     */
    public function dan($myCards)
    {
        $num = count($myCards);
        if ($num == 1) {
            return MessageCode::$STATUS_SUCCESS_CARD;

        } else {
            return MessageCode::$STATUS_ERROR_CARDTYPE;
        }

    }

    /**
     * 对子
     * @param $myCards
     * @return int
     */
    public function duiZi($myCards)
    {
        $num = count($myCards);
        if ($num == 2) {
            $grade1 = $this->cardDetail->getGrade($myCards[0]['cardId']);
            $grade2 = $this->cardDetail->getGrade($myCards[1]['cardId']);
            if ($grade1 == $grade2) {
                return MessageCode::$STATUS_SUCCESS_CARD;
            } else {
                return MessageCode::$STATUS_ERROR_CARDTYPE;
            }
        }
        return MessageCode::$STATUS_ERROR_CARDTYPE;
    }

    /**
     * 3不带
     * @param $myCards
     * @return int
     */
    public function sanBuDai($myCards)
    {
        $num = count($myCards);
        if ($num == 3) {
            $grade1 = $this->cardDetail->getGrade($myCards[0]['cardId']);
            $grade2 = $this->cardDetail->getGrade($myCards[1]['cardId']);
            $grade3 = $this->cardDetail->getGrade($myCards[2]['cardId']);
            if ($grade1 == $grade2 && $grade1 == $grade3) {
                return MessageCode::$STATUS_SUCCESS_CARD;
            } else {
                return MessageCode::$STATUS_ERROR_CARDTYPE;
            }
        }
        return MessageCode::$STATUS_ERROR_CARDTYPE;

    }

    /**
     * 炸弹
     * @param $myCards
     * @return int
     */
    public function zhanDan($myCards)
    {
        $num = count($myCards);

        if ($num == 4) {
            $grade1 = $this->cardDetail->getGrade($myCards[0]['cardId']);
            $grade2 = $this->cardDetail->getGrade($myCards[1]['cardId']);
            $grade3 = $this->cardDetail->getGrade($myCards[2]['cardId']);
            $grade4 = $this->cardDetail->getGrade($myCards[3]['cardId']);
            if ($grade1 == $grade2 && $grade1 == $grade3 && $grade1 == $grade4) {
                return MessageCode::$STATUS_SUCCESS_CARD;
            } else {
                return MessageCode::$STATUS_ERROR_CARDTYPE;
            }
        }
        return MessageCode::$STATUS_ERROR_CARDTYPE;

    }

    /**
     * 3带一
     * @param $myCards
     * @return int
     */
    public function sanDaiyi($myCards)
    {
        $num = count($myCards);
        if ($num == 4) {
            $myCards = $this->cardDetail->arrSort($myCards, 'grade');
            $grades = [];
            for ($i = 0; $i < 4; $i++) {
                $grades[$i] = $this->cardDetail->getGrade($myCards[$i]['cardId']);
            }
            //排除炸弹
            if (($grades[1] == $grades[0]) && ($grades[2] == $grades[0])
                && ($grades[3] == $grades[0])) {
                return MessageCode::$STATUS_ERROR_CARDTYPE;
            }
            //单张在前
            if ($grades[1] == $grades[2] && $grades[1] == $grades[3]) {
                return MessageCode::$STATUS_SUCCESS_CARD;
            }
            //单张在尾
            if ($grades[0] == $grades[1] && $grades[1] == $grades[2]) {
                return MessageCode::$STATUS_SUCCESS_CARD;
            }
            return MessageCode::$STATUS_ERROR_CARDTYPE;
        }
        return MessageCode::$STATUS_ERROR_CARDTYPE;
    }

    /**
     * 三带二
     * @param $myCards
     * @return int
     */
    public function sanDaiEr($myCards)
    {
        $num = count($myCards);
        if ($num == 5) {
            $myCards = $this->cardDetail->arrSort($myCards, 'grade');
            for ($i = 0; $i < 5; $i++) {
                $grades[$i] = $this->cardDetail->getGrade($myCards[$i]['cardId']);
            }
            //排除顺子
            if ($grades[1] - $grades[0] == 0 || $grades[4] - $grades[3] == 0) {

                //对子在前
                if ($grades[0] == $grades[1] && $grades[2] == $grades[3] && $grades[2] == $grades[4]) {
                    return MessageCode::$STATUS_SUCCESS_CARD;
                }
                //对子在尾
                if ($grades[0] == $grades[1] && $grades[0] == $grades[2] && $grades[3] == $grades[4]) {
                    return MessageCode::$STATUS_SUCCESS_CARD;
                }
            }
            return MessageCode::$STATUS_ERROR_CARDTYPE;
        }
        return MessageCode::$STATUS_ERROR_CARDTYPE;
    }

    /**
     * 顺子
     * @param $myCards
     * @return int
     */
    public function shunZi($myCards)
    {
        $num = count($myCards);
        if ($num >= 5 && $num <= 12) {
            $myCards = $this->cardDetail->arrSort($myCards, 'grade');
            for ($i = 0; $i < $num; $i++) {
                $arr1 = $this->cardDetail->getGrade($myCards[$i]['cardId']);
                $arr2 = $this->cardDetail->getGrade($myCards[$i + 1]['cardId']);
                //排除大小王
                if (in_array($arr2, [16, 17]) || in_array($arr1, [15, 16])) {
                    return MessageCode::$STATUS_ERROR_CARDTYPE;
                }
                //验证顺子
                if ($arr2 - $arr1 != 1) {
                    return MessageCode::$STATUS_ERROR_CARDTYPE;
                }

            }
            return MessageCode::$STATUS_SUCCESS_CARD;
        }
        return MessageCode::$STATUS_ERROR_CARDTYPE;
    }

    /**
     * 4带二
     * @param $myCards
     * @return int
     */
    public function siDaiEr($myCards)
    {
        $num = count($myCards);
        if ($num == 6) {
            $myCards = $this->cardDetail->arrSort($myCards, 'grade');
            $grades = [];
            for ($i = 0; $i < $num; $i++) {
                $grades[$i] = $this->getGrade($myCards[$i]['cardId']);
            }
            //四张在前
            if ($grades[0] == $grades[1] && $grades[0] == $grades[2] && $grades[0] == $grades[3]) {
                return MessageCode::$STATUS_SUCCESS_CARD;
            }

        }

    }

    /**
     * 连对
     * @param $myCards
     * @return int
     */
    public function lianDui($myCards)
    {
        $num = count($myCards);
        if ($num >= 6 && $num % 2 == 0) {
            $myCards = $this->cardDetail->arrSort($myCards, 'grade');
            for ($i = 0; $i < $num; $i = $i + 2) {
                if ($this->cardDetail->getGrade($myCards[$i]['cardId']) != $this->cardDetail->getGrade($myCards[$i + 1]['cardId'])) {
                    return MessageCode::$STATUS_ERROR_CARDTYPE;

                }
                if ($this->cardDetail->getGrade($myCards[$i]['cardId']) != $this->cardDetail->getGrade($myCards[$i + 2]['cardId'] - 1)) {
                    return MessageCode::$STATUS_ERROR_CARDTYPE;
                }
            }
            return MessageCode::$STATUS_SUCCESS_CARD;


        }
        return MessageCode::$STATUS_ERROR_CARDTYPE;

    }

    /**
     * 王炸
     * @param $myCards
     */
    public function wangZha($myCards)
    {
        $num = count($myCards);
        if ($num == 2) {
            $grade1 = $this->cardDetail->getGrade($myCards[0]['cardId']);
            $grade2 = $this->cardDetail->getGrade($myCards[1]['cardId']);
            if ($grade2 + $grade1 == 33) {
                return MessageCode::$STATUS_SUCCESS_CARD;
            }
            return MessageCode::$STATUS_ERROR_CARDTYPE;
        }
        return MessageCode::$STATUS_ERROR_CARDTYPE;
    }

    /**
     * 飞机不带
     * @param $myCards
     * @return int
     */
    public function feiJiBuDai($myCards)
    {
        $num = count($myCards);
        $n = $num / 3;
        if ($num >= 6 && $num % 3 == 0) {
            //判断每连续的三张是否是相等
            for ($i = 0; $i < $n; $i++) {
                $arr = array_slice($myCards, $i * 3, $i * 3 + 3);
                $status = $this->sanBuDai($arr);
                if ($status == 1013) {
                    return MessageCode::$STATUS_ERROR_CARDTYPE;

                } else {
                    //如果相等，记住其中一张牌的等级
                    $grades[$i] = $this->cardDetail->getGrade($myCards[$i * 3]['cardId']);
                }
            }

            //排除三个2
            for ($i = 0; $i < $n - 1; $i++) {
                if ($grades[$i] == 15) {
                    return MessageCode::$STATUS_ERROR_CARDTYPE;
                }
                //连续两坎直接是连续的
                if ($grades[i + 1] - $grades[i] != 1) {
                    return MessageCode::$STATUS_ERROR_CARDTYPE;

                }
            }
            return MessageCode::$STATUS_SUCCESS_CARD;

        }
        return MessageCode::$STATUS_ERROR_CARDTYPE;
    }
    //飞机带两张
    //飞机带两对

}